package com.example.demo.controller.Test;

import com.example.demo.pojo.RecruitInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
public class RedisTestController {
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/getredis")
    public String getredis(){
        Object ob = redisTemplate.opsForValue().get("3");
        System.out.println(ob);
        return "success";
    }

    @GetMapping("/setredis")
    public String setredis(){
        redisTemplate.opsForValue().set("3","4");
        //        //清理缓存
//        RedisTemplate redisTemplate = new RedisTemplate<>();
//        redisTemplate.delete("studentInfo");
        redisTemplate.delete("studentInfo");
        return "success";
    }
    //清理缓存
    @CacheEvict(value = "recruitCache",key = "#id")//key="#p0"or"root.args[0]"
    @GetMapping("/deleteCache/{id}")//这里的id是key = 123,因为当时存的是123
    public void delete(@PathVariable Long id){

    }

    @CachePut(value = "userCache",key = "#recruitInfo.account")//redis保存为value:key,文件内是真value,前面那个value只是名字
    @PostMapping("/saveCache")
    public RecruitInfo save(@RequestBody RecruitInfo recruitInfo){
        System.out.println(recruitInfo);
        return recruitInfo;
    }

    @Cacheable(value = "userCache",key = "#id",condition = "#result != null")//查询结果不为空才返回数据
    @GetMapping("/getByIdCache/{id}")
    public RecruitInfo getById(@PathVariable String id){
//        RecruitInfo recruitInfo = recruitInfoService.getById(id);
        RecruitInfo recruitInfo = new RecruitInfo();//这个也可以,一般是上面那个才正确
        return recruitInfo;
    }
}
